package com.zfl.mapper;

import com.zfl.entity.User;
import org.apache.ibatis.annotations.*;

@Mapper
public interface UserMapper {

    // 其他已有方法...
    @Select("SELECT * FROM user WHERE username = #{username}")
    User selectByUsername(String username);

    @Select("SELECT role_id FROM user_role WHERE user_id = #{userId}")
    Long selectRoleIdByUserId(Long userId);

    @Select("SELECT COUNT(*) FROM user WHERE username = #{username}")
    int countByUsername(String username);

    @Insert("INSERT INTO user (username, password, status, password_try_count, ban_remaining_time, create_time) " +
            "VALUES (#{username}, #{password}, #{status}, #{passwordTryCount}, #{banRemainingTime}, #{createTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id") // 新增这行：自增ID回填到 user 对象的 id 字段
    void insert(User user);

    @Insert("INSERT INTO user_role(user_id, role_id) VALUES (#{userId}, #{roleId})")
    void insertUserRole(Long userId, Long roleId);


    // 新增：根据ID更新用户信息（解决编译错误的核心）
    @Update("UPDATE user SET " +
            "username = #{username}, " +
            "password = #{password}, " +
            "status = #{status}, " +
            "password_try_count = #{passwordTryCount}, " +
            "ban_remaining_time = #{banRemainingTime}, " +
            "create_time = #{createTime} " +
            "WHERE id = #{id}")
    void updateById(User user);
}